Migo商城2.0 图片上传代码实现 六

Migo商城 图片上传代码实现 六

图片服务器搭建完成后,开始回到商品的图片上传功能的实现

先来分析页面代码:

图片上传参数

请求的url:/rest/pic/upload

参数:MultiPartFileuploadFile

上传文件返回的数据是:文本类型的json数据

查文档http://kindeditor.net/docs/upload.html

1
2
3
4
5
6
7
8
9
10
11
12
返回格式(JSON)

//成功时
{
"error" : 0,
"url" : "http://www.example.com/path/to/file.ext"
}
//失败时
{
"error" : 1,
"message" : "错误信息"
}

这里使用使用Map设置返回值(请对比migo第一版实现,用的是POJO来做封装,这里换一种方法)

接收的参数:MulitPartFileuploadFile

业务逻辑:把图片流上传到图片服务器,使用FastDFSClient工具类上传,返回图片的url。需要把图片服务器的域名或者ip拼接返回的url做为完整的url返回。把urlerror 对象封装到map中返回。

返回值:map

添加依赖:

1
2
3
4
5
<dependency>
<groupId>fastdfs_client</groupId>
<artifactId>fastdfs_client</artifactId>
<version>1.25</version>
</dependency>

在common工程中加入工具类 FastDFSClient.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.migo.utils;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

public class FastDFSClient {

private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;

public FastDFSClient(String conf) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}

/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}

public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
}

public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
}

/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {

String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
}

public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}

public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
}

service实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.migo.service;

import com.migo.utils.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
* 图片上传
* Author 知秋
* Created by kauw on 2016/11/11.
*/
@Service
public class PicService {
@Value("${IMAGE_SERVER_BASE_URL}")
private String IMAGE_SERVER_BASE_URL;

public Map uploadFile(MultipartFile uploadFile){

try {
FastDFSClient client=new FastDFSClient("classpath:properties/env.properties");
//获取图片原始名称
String filename = uploadFile.getOriginalFilename();
//取扩展名
String extendName = filename.substring(filename.lastIndexOf(".") + 1);
String url=client.uploadFile(uploadFile.getBytes(),extendName);
//拼接URL
url=IMAGE_SERVER_BASE_URL+url;
//返回map数据
Map result=new HashMap();
result.put("error",0);
result.put("url",url);
return result;
} catch (Exception e) {
e.printStackTrace();
//返回Map数据
Map result = new HashMap<>();
result.put("error", 1);
result.put("message", "上传失败");

return result;

}

}
}

Controller实现:

在springmvc中上传图片,需要在springmvc.xml中配置多媒体解析器

上传文件返回的数据是:文本类型的json数据 所以在controller里要将map数据转换为字符串返回

jsonutils是封装的工具类,很简单,就是封装Jackson的实现方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.migo.controller;

import com.migo.service.PicService;
import com.migo.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
* 图片上传controller
* Author 知秋
* Created by kauw on 2016/11/11.
*/
@Controller
public class PicController {
@Autowired
private PicService picService;

@RequestMapping("/rest/pic/upload")
@ResponseBody
public String uploadFile(MultipartFile uploadFile){
Map result = picService.uploadFile(uploadFile);

//把Java对象手工转换成json数据
String json = JsonUtils.objectToJson(result);
return json;

}
}

到此,图片上传功能完成,具体效果图后面再展示

您的支持将鼓励我继续创作!